function check() {
    document.querySelector("#myCheck").checked = true;
    document.getElementById('fwtc').style.display = 'none';
}

function uncheck() {
    document.querySelector("#myCheck").checked = false;
    document.getElementById('fwtc').style.display = 'none';
}
//下拉框选中改变颜色
$(function () {
    var unSelected = "#999999";
    var selected = "#333333";
    $("select").css("color", unSelected);
    $("option").css("color", selected);
    $("select").change(function () {
        var selItem = $(this).val();
        if (selItem == $(this).find('option:first').val()) {
            $(this).css("color", unSelected);
        } else {
            $(this).css("color", selected);
        }
    });
});


$(function () {
    var page_id = $('#page_id').val();
    //获取用户协议
    $.ajax({
        type: "GET",                      //请求类型
        url: "https://yinshuapi.scyushun.com/api/agreement",           //URL
        data: { id: page_id },   //传递的参数
        dataType: "json",                 //返回的数据类型
        success: function (data) {
            $('#agreement').empty();
            $('#agreement').append(data.data)
        }
    });
})

var phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;//手机号正则
var count = 60; //间隔函数，1秒执行
var InterValObj1; //timer变量，控制时间
var curCount1;//当前剩余秒数
/*第一*/
function sendMessage() {
    curCount1 = count;
    var mobile = $.trim($('#mobile').val());

    if (!phoneReg.test(mobile)) {
        layer.msg("请输入有效的手机号码");
        return false;
    }
    //设置button效果，开始计时
    var page_id = $('#page_id').val();
    $("#btnSendCode1").attr("disabled", "true");
    $("#btnSendCode1").val(+curCount1 + "秒再获取");
    $.post('https://yinshuapi.scyushun.com/api/send_sms', { phone: mobile, page_id: page_id }, function (data) {
        if (data.code == 200) {
            $("#btnSendCode1").attr("disabled", "true");
            $("#btnSendCode1").val(+curCount1 + "秒再获取");
            InterValObj1 = window.setInterval(SetRemainTime1, 1000); //启动计时器，1秒执行一次
        } else if (data.code == 500) {
            layer.msg("验证码错误");
            return false;
        } else {
            layer.msg("手机验证码获取上限");
            return false;
        }
    });
}
function SetRemainTime1() {
    if (curCount1 == 0) {
        window.clearInterval(InterValObj1);//停止计时器
        $("#btnSendCode1").removeAttr("disabled");//启用按钮
        $("#btnSendCode1").val("重新发送");
    } else {
        curCount1--;
        $("#btnSendCode1").val(+curCount1 + "秒再获取");
    }
}
function provinceChange() {
    var $pro = $("#selProvince");
    var parent_id = $pro.val();
    var $sel = $("#selCity");
    $sel.empty();
    var city_ids = '1093,1022,1057,1032,1144,1184,1158,3320,2163,2083,2038,2122,2097,2199,2070,2242,2876,2368,123,38,1529,1506,650' +
        '1756,1698,1927,2009,1953,1901,580,842,875,868,811,859,849,914,907,823,831,883,893,900,1239,479,465,349,3480,1351,1421,1453' +
        '1362,1408,1395,1373,218,3202,3234,2671,20,3508,2974,921,966,958,979,935,972,1001,946,2630';
    $.ajax({
        type: "GET",                      //请求类型
        url: "https://yinshuapi.scyushun.com/api/customize_city",           //URL
        data: {parent_id: parent_id,city_ids:city_ids},   //传递的参数
        dataType: "json",                 //返回的数据类型
        success: function (data) {
            //给省份下拉列表赋值
            var option = '';
            $.each(data.data, function (index, item) {
                option += "<option value='" + item.id + "'>" + item.name + "</option>";
            });
            $sel.append(option);
        }
    });
}
/*提交*/
function submitForm() {
    var zz = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
    var mobile = $('#mobile').val();
    var code = $('#code').val();
    var selProvince = $('#selProvince').find("option:selected").text();
    var city = $('#selCity').find("option:selected").text();
    var city_id = $('#selCity').val();
    var url = window.location.href;
    var page_id = $('#page_id').val();
    var remark = '';
    var name = $('#name').val();
    var money = $('#amount').val();
    if (city_id == '') {
        layer.msg("请选择城市");
        return false;
    }
    if (mobile == '') {
        layer.msg("填写手机号");
        return false;
    }
    if (code == '') {
        layer.msg("请填写验证码");
        return false;
    }
    if (code.length != 6) {
        layer.msg("请填写正确6位数验证码");
        return false;
    }
    if (name == '') {
        layer.msg("姓名为必填！");
        return false;
    }
    if (money == '') {
        layer.msg("请选择您的需求金额！");
        return false;
    }



        var cartype = $('#cartype').val(); // 车辆型号
        var price = $('#price').val(); // 裸车价
        var carmodel = $('#carmodel').val();//车辆型号

        if (cartype == '') {
            layer.msg("请选填写车辆型号！");
            return false;
        }
        if (price == '') {
            layer.msg("请选填写裸车价！");
            return false;
        }
    if (carmodel == '') {
        layer.msg("请选填车辆型号！");
        return false;
    }
        remark = remark+'车辆型号:' + cartype + ',&裸车价:' + price + ',&车辆型号:' + carmodel + ',&资金需求:' + money;

    if (!$('#myCheck').get(0).checked) {
        layer.msg("请勾选同意用户协议！");
        return false;
    }
    if (zz.test(mobile)) {
        $.ajax({
            type: "POST",                      //请求类型
            url: "https://yinshuapi.scyushun.com/api/add_user",           //URL
            data: {
                mobile: mobile,
                city: city,
                city_id: city_id,
                code: code,
                page_id: page_id,
                url: url,
                remark: remark,
                name: name,
                money: money,
                cartype:cartype,
                price:price,
                carmodel:carmodel
            },   //传递的参数
            dataType: "json",                 //返回的数据类型
            success: function (data) {
                if (data.code == 300) {
                    layer.msg("验证码错误");
                    return false;
                } else if (data.code == 600) {
                    layer.msg("请勿重复提交");
                    return false;
                } else if (data.code == 200) {
                    layer.msg("申请成功，我司将提供免费贷款方案，通过电话联系您，请注意接听！银树科技，助贷大平台，让你贷款无忧！！");
                    return true;
                }else {
                    layer.msg(data.message);
                    return false;
                }
            },
            error: function (data) {
                layer.msg("申请成功，我司将提供免费贷款方案，通过电话联系您，请注意接听！银树科技，助贷大平台，让你贷款无忧！！");
                return true;
            }
        });
    } else {
        layer.msg("手机号码格式错误");
        return false;
    }
}



